package com.qkm.xinguan.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qkm.xinguan.domain.entity.Notice;
import com.qkm.xinguan.domain.infrastructure.mapper.NoticeMapper;
import com.qkm.xinguan.domain.infrastructure.utils.UserUtil;
import com.qkm.xinguan.domain.transform.dto.NoticeDTO;
import com.qkm.xinguan.domain.transform.form.NoticeSearchForm;
import com.qkm.xinguan.exception.BusinessException;
import com.qkm.xinguan.repository.NoticeRepository;
import com.qkm.xinguan.response.Result;
import com.qkm.xinguan.response.ResultCode;
import com.qkm.xinguan.vo.NoticeVo;
import com.qkm.xinguan.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Slf4j
@Service
public class NoticeRepositoryImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeRepository {

	/**
	 * 检测公告是否存在
	 *
	 * @param noticeTip 公告名
	 * @return boolean
	 */
	@Override
	public boolean checkNoticeExist(String noticeTip) {
		LambdaQueryWrapper<Notice> wrapper=new LambdaQueryWrapper<>();
		wrapper.eq(Notice::getNoticeTip,noticeTip);
		return this.baseMapper.selectCount(wrapper)>0;
	}


	/**
	 * 分页获取用户列表
	 *
	 * @param page 页数
	 * @param size 条数
	 * @return Page<Notice>
	 */
	@Override
	public Result getNoticeListPage(Integer page, Integer size) {
		QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
		queryWrapper.select().orderByDesc("modified_time");
		Page<Notice> p= this.baseMapper.selectPage(new Page<>(page, size), queryWrapper);
		return Result.ok().data(p);
	}

	/**
	 * 条件查询公告列表，并且分页
	 *
	 * @param noticeSearchForm 用户查询框
	 * @param page             页码
	 * @param size             数量
	 * @return Page<UserVO>
	 */
	@Override
	public Page<Notice> searchNoticeListPage(NoticeSearchForm noticeSearchForm, Integer page, Integer size) {
		LambdaQueryWrapper<Notice> wrapper = executeWrapper(noticeSearchForm);
		Page<Notice> pageInfo = this.baseMapper.selectPage(new Page<>(page, size), wrapper);
		return pageInfo;
	}

	/**
	 * @param id
	 * @param noticeDTO
	 * @return
	 */
	@Override
	public Result update(Long id, NoticeDTO noticeDTO) {
		Notice notice=this.baseMapper.selectById(id);
		if(Objects.isNull(notice)){
			throw new BusinessException(ResultCode.PARAM_ERROR, "来源信息不存在，请重试！");
		}
		BeanUtils.copyProperties(noticeDTO,notice);
		int res=this.baseMapper.updateById(notice);
		if(res>0){
			return Result.ok();
		}
		throw new BusinessException(ResultCode.FAILED);
	}

	private static LambdaQueryWrapper<Notice> executeWrapper(NoticeSearchForm noticeSearchForm) {
		LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
		if (null != noticeSearchForm.getNoticeTip()) {
			wrapper.eq(Notice::getNoticeTip, noticeSearchForm.getNoticeTip());
		}
		if (!StringUtils.isEmpty(noticeSearchForm.getNoticeMessage())) {
			wrapper.eq(Notice::getNoticeMessage, noticeSearchForm.getNoticeMessage());
		}
		return wrapper;
	}
}
